<?php

defined('BASEPATH') or die('forbidden');

class mallclassification_Controller Extends Controller {

    private $inc;

    function _init() {
        check_level(50, segment(0), segment(1, 'index'));

        $this->inc = $this->config['inc'];
    }

    function index() {
        //print_r($_SESSION['tj']);
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        if ($area_host == '') {
            echo "<script>alert('" . translate('C_SELECT_AREA_TEXT') . "');</script>";
        }
        $this->view->display("mall/mallclassification/index");
    }

    function flexigrid() {
        //读取flexigrid传输的数据
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $strResult = vpost($area_host . "action=38");
        $rowsData = json_decode($strResult, true);
        //print_r($rowsData);
        unset($areaarray);
        $areaarray = get_area_list_host();
        $kindtypeList = get_type_shop();
        $i = 1;
        //$_SESSION['tj']=$offset.'*'.$rp;
        foreach ($rowsData as $key => $value) {
            if ($i >= $offset && $i <= ($rp + $offset)) {
                $value['area_host'] = $areaarray[$area_host];
                //$value['kindname'] = $kindtypeList[$value['kindid']];
                $id = $area_host . "id=" . $value['id'] . '&action=40';
                $id = base64_encode($id);
                $rows[] = array('id' => $id, 'cell' => $value);
            }
            $i++;
        }
        $total = $i;
        
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        //是否来源于表单请求
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $area_host = $rowdata['area_host'];
            $rowdata['deadtime'] = strtotime($rowdata['deadtime']);
            unset($rowdata['area_host']);
            $str = array_to_stringparam($rowdata);
            $str = $area_host . $str . "action=39";
            $strResult = vpost($str);

            $result = 0;
            //操作失败或者连接失败
            if ($strResult != '0' || $strResult === FALSE)
            {
                $result = 1;
            }
            else
            {
                insert_log(translate('MENU_MALL_MALLCLASSIFICATION'), translate('ADD'),NULL, $rowdata);
            }
            cp_tips($result == 0 ? "success" : "error");
        }
        
        //$kindObject = array();	//物品分类信息
        $hotlimitObject = array();	//限时抢购物品
        
        $area_host = !empty($_SESSION['area_selected_host']) ? $_SESSION['area_selected_host'] : '';
        $strResult = vpost($area_host . "action=28");
        $rowsData = json_decode($strResult, true);
        $rows = array();
        foreach ($rowsData as $key => $value) {
            $rows[$value['id']]=$value['name'];
            //print_r($value);
            //$kindObject[$value['kindid']][$value['id']]=$value['name'];
            if(1 == $value['hotlimit'])
            {
            	$hotlimitObject[$value['id']]=$value['name'];
            }
        }
      
        $this->view->assign('area_host', $this->form->select('row[area_host]', get_area_list_host(), '', array('id' => 'area_host')));
        $this->view->assign("kindid",get_type_shop());	//物品id与name对应表
        $this->view->assign("shopidName",$rows);	//全部数据
		$this->view->assign("hotlimitObject",$hotlimitObject);	//限时抢购
		//$this->view->assign("kindObject",$kindObject);	//分类数据	待用
        $this->view->display("mall/mallclassification/add");
    }

    function del($id = 0) {
        $c = 0;
        $r = '';
        if (!isset($id))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $str = base64_decode($id);
        $strResult = vpost($str);
        $result = 0;
        //操作失败或者连接失败
        if ($strResult != '0' || $strResult === FALSE) {
            $result = 1;
            $r = translate('C_TIPS_ERROR_TEXT');
        } else {
            insert_log(translate('MENU_MALL_MALLCLASSIFICATION'),translate('DEL'), array('id'=>$id));
            $c = 1;
            $r = translate('C_TIPS_SUCCESS_TEXT');
        }
        json_output($c, $r);
    }

}
